package org.dcarew.stripjar;

import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;

import java.io.File;
import java.io.OutputStream;

public class StripperOptions {
  private String[] args;
  private CmdLineParser parser;

  @Option(name = "--help", //
  usage = "prints this help message")
  private boolean showHelp = false;

  @Option(name = "--version", //
  usage = "print the stripjar version")
  private boolean showVersion = false;

  @Option(name = "--out", //
  usage = "the output file", //
  metaVar = "<file>")
  private File outFile = null;

  @Option(name = "--nostrip", //
  usage = "a regular expression to match classes not to strip")
  private String noStrip = null;

  @Option(name = "--reorder", //
  usage = "reorder zip entries according to the given -verbose:class output", //
  metaVar = "<file>")
  private File reorderFile = null;

  @Argument
  private final File file = null;

  public StripperOptions() {
    this(new String[0]);
  }

  public StripperOptions(String[] args) {
    this.args = args;

    parser = new CmdLineParser(this);
    parser.setUsageWidth(100);
  }

  public File getFile() {
    return file;
  }

  public String getNoStrip() {
    return noStrip;
  }

  public File getOutFile() {
    if (outFile != null) {
      return outFile;
    } else {
      File file = getFile();
      String name = file.getName();

      int index = name.lastIndexOf('.');

      if (index != -1) {
        name = name.substring(0, index);
      }

      name += "-min.jar";

      return new File(file.getParentFile(), name);
    }
  }

  public File getReorderFile() {
    return reorderFile;
  }

  public boolean getShowHelp() {
    return showHelp;
  }

  public boolean getShowVersion() {
    return showVersion;
  }

  public void parse() throws CmdLineException {
    parser.parseArgument(args);
  }

  public void printUsage(OutputStream out) {
    parser.printUsage(out);
  }

}
